public class SyncMethod {
    public int i;
    
    // Synchronizing the method to ensure only one thread can execute this at a time.
    public synchronized void syncTask(){
        i++;
    }
}

public class SyncCodeBlock {
    public int i;

    public void syncTask(){
        // Only the critical section of code that needs synchronization is locked, potentially improving performance.
        synchronized (this){
            i++;
        }
    }
}